package com.ovopark.firecar.recyclerview.viewholder;


import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.ovopark.firecar.recyclerview.callback.OnClickListenerCallBack;


/**
 * @author Created by DaXian on 2018/6/18.

 */

public class BaseRecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private static final String TAG = "BaseRecyclerViewHolder";
    private SparseArray<View> mSparseArray = new SparseArray<>();
    private View mView;
    private OnClickListenerCallBack mCallBack;
    public int mPosition = -1;
    private Context mContext;


    public BaseRecyclerViewHolder(View itemView, int position, int i, Context context, OnClickListenerCallBack callBack) {
        super(itemView);
        itemView.setOnClickListener(this);
        mView = itemView;
        mCallBack = callBack;
        mPosition = position;
        mContext = context;
    }

    public <T extends View> T getView(int viewId) {
        View view = mSparseArray.get(viewId);
        if (view != null) {
            return (T) view;
        }
        return findView(viewId);
    }

    private <T extends View> T findView(int viewId) {
        if (mView != null) {
            View view = mView.findViewById(viewId);
            if (view != null) {
                mSparseArray.put(viewId, view);
                return (T) view;
            }
            Log.e(TAG, "findView: this view is not found");
        }
        Log.e(TAG, "findView: mView is null");
        return null;
    }

    public View getConVerView() {
        return mView;
    }

    public BaseRecyclerViewHolder setText(@IdRes int viewId, CharSequence text) {
        TextView textView = getView(viewId);
        if (textView != null) {
            if (text == null) {
                text = "";
            }
            textView.setText(text);
        }
        return this;
    }

    public BaseRecyclerViewHolder setText(@IdRes int viewId, @StringRes int text) {
        TextView textView = getView(viewId);
        if (textView != null) {
            textView.setText(text);
        }
        return this;
    }

    public BaseRecyclerViewHolder setImage(@IdRes int viewId, int imgId) {
        ImageView imageView = getView(viewId);
        if (imageView != null) {
            imageView.setImageResource(imgId);
        }
        return this;

    }
//    public BaseRecyclerViewHolder setImage(@IdRes int viewId, String imgUrl) {
//        ImageView imageView = getView(viewId);
//        if (imageView != null) {
//            GlideUtils.create(mContext, imgUrl, imageView);
//        }
//        return this;
//
//    }


    /**
     * 设置 view 是否显示
     *
     * @param viewId
     * @param visibility
     */
    public BaseRecyclerViewHolder setVisibility(@IdRes int viewId, boolean visibility) {
        View view = getView(viewId);
        if (view != null) {
            view.setVisibility(visibility ? View.VISIBLE : View.GONE);
        }
        return this;
    }

    /**
     * 设置 view 是否显示
     *
     * @param viewId
     * @param visibility
     */
    public BaseRecyclerViewHolder setVisibility(@IdRes int viewId, int visibility) {
        View view = getView(viewId);
        if (view != null) {
            view.setVisibility(visibility);
        }
        return this;
    }

    public BaseRecyclerViewHolder setListener(@IdRes int viewId) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnClickListener(this);
        }
        return this;
    }

    public BaseRecyclerViewHolder setTextColor(@IdRes int viewId, @ColorRes int color) {
        TextView textView = getView(viewId);
        if (textView != null) {
            textView.setTextColor(ContextCompat.getColor(mContext, color));
        }
        return this;
    }

    @Override
    public void onClick(View v) {
        mCallBack.onClickListenerCallBack(mPosition, mView, BaseRecyclerViewHolder.this);
    }


}
